package com.turing.post.organization.impl;

import java.math.BigInteger;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;

import com.turing.post.common.util.SysOperLog;
import com.turing.post.common.util.SysOperLogDao;
import com.turing.post.organization.util.OrganApplicationException;
import com.turing.post.organization.util.OrganRespCode;
import com.turing.post.organization.util.Voucher;
import com.turing.post.organization.util.VoucherBusiness;
import com.turing.post.organization.util.VoucherDao;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;
import com.turing.post.util.PCPSWebInput;

/**
 * @author wzd
 * @Dec 18, 2012
 */
@Stateless(mappedName = "organization.impl.VoucherBusinessImpl")
@Remote
@Local
public class VoucherBusinessImpl implements VoucherBusiness {

	@EJB(mappedName = "organization.impl.VoucherDaoImpl")
	private VoucherDao VoucherDaoImpl;

	@EJB(mappedName = "common.impl.SysOperLogDaoImpl")
	private SysOperLogDao sysOperLogDaoImpl;

	public void insertVoucher(PCPSWebInput webInput, Voucher voucher)
			throws PCPSApplicationException {
		// 登录用户机构号为空
		if (PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
			throw new OrganApplicationException(
					OrganApplicationException.ERRCODE_CATEGORY_APPLICATION,
					OrganRespCode.ISSUE_INSERT_UPDATE, "登录用户机构号为NULL");
		}
		// 发卡商编号
		if (PCPSUtil.isEmpty(voucher.getIssueCompanyNo())) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
					"发卡商编号为空!");
		}
		// 名称
		if (PCPSUtil.isEmpty(voucher.getVoucherName())) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
					"凭证种类为空!");
		}
		// 发卡商机构NO
		if (voucher.getVoucherNoLen() == null) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
					"凭证号长度不能等于0!");
		}
		// 得到主键id
		BigInteger voucherId = VoucherDaoImpl.getMaxIdVoucherId();
		if (voucherId == null) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
					"插入失败得到主键Id为空!");
		}
		List<Voucher> voucherlist = VoucherDaoImpl.getVoucherByVoucherName(
				PCPSUtil.getIssueCompanyNo(webInput), voucher
						.getVoucherName());
		if (voucherlist != null && voucherlist.size() > 0) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
					"凭证名称已经存在!");
		}
		// 插入数据
		Voucher voucher1 = new Voucher();
		voucher1.setImageId(voucher.getImageId());
		voucher1.setIssueCompanyNo(voucher.getIssueCompanyNo());
		voucher1.setRemark(voucher.getRemark());
		voucher1.setVoucherId(voucherId);
		voucher1.setVoucherName(voucher.getVoucherName());
		voucher1.setVoucherNoLen(voucher.getVoucherNoLen());

		VoucherDaoImpl.insertVoucher(voucher1);
		// 插入数据库操作日志
		insertRecordingOperationLog(webInput, "凭证种类", voucher1,
				SysOperLog.operType_Add, "VoucherName:"
						+ voucher1.getVoucherName());
	}

	public void updateVoucher(PCPSWebInput webInput, Voucher voucher)
			throws PCPSApplicationException {
		// 登录用户机构号为空
		if (PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
			throw new OrganApplicationException(
					OrganApplicationException.ERRCODE_CATEGORY_APPLICATION,
					OrganRespCode.ISSUE_INSERT_UPDATE, "登录用户机构号为NULL");
		}
		// 凭证种类Id
		if (voucher.getVoucherId() == null) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
					"凭证种类Id为空!");
		}
		// 发卡商编号
		if (PCPSUtil.isEmpty(voucher.getIssueCompanyNo())) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
					"发卡商编号为空!");
		}
		// 名称
		if (PCPSUtil.isEmpty(voucher.getVoucherName())) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
					"凭证种类为空!");
		}
		// 发卡商机构NO
		if (voucher.getVoucherNoLen() == null) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
					"凭证号长度不能等于0!");
		}
		// 得到主键id
		BigInteger voucherId = VoucherDaoImpl.getMaxIdVoucherId();
		if (voucherId == null) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
					"插入失败得到主键Id为空!");
		}

		VoucherDaoImpl.updateVoucher(voucher);
		// 插入数据库操作日志
		insertRecordingOperationLog(webInput, "修改凭证种类", voucher,
				SysOperLog.operType_Add, "VoucherName:"
						+ voucher.getVoucherName());
	}

	private void insertRecordingOperationLog(PCPSWebInput webInput,
			String logName, Voucher obj, String operType, String lastFieldName)
			throws PCPSApplicationException {
		String logRemark = PCPSUtil.RecordingOperationLog(obj, lastFieldName);
		if (!PCPSUtil.isEmpty(logRemark)) {
			// 插入数据库日志
			SysOperLog sysOperLog = new SysOperLog();
			if (webInput != null && webInput.getOperId() != null) {
				sysOperLog.setOperId(webInput.getOperId());
				sysOperLog.setOperType(operType);
				sysOperLog.setLogName(logName);
				if (webInput != null) {
					sysOperLog.setIpAddress(webInput.getIpAddress());
				}
				if (!PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
					sysOperLog.setIssueCompanyNo(webInput.getOperBranchNo()
							.substring(0, 6));
					sysOperLog.setBranchNo(webInput.getOperBranchNo());
				}
				sysOperLog.setRemark(logRemark);
				sysOperLogDaoImpl.insertSysOperLog(sysOperLog);
			}
		}
	}
}
